package com.oscar.ehome.utils.view.cutimagview;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.oscar.ehome.R;
import com.oscar.ehome.base.activity.BaseActivity;
import com.oscar.ehome.base.activity.BasesActivity;
import com.oscar.ehome.presenter.BasePresenter;
import com.oscar.ehome.utils.system.SystemConfigFactory;
import com.oscar.ehome.view.teamview.TeamDetailActivity;

import net.tycmc.bulb.androidstandard.utils.JsonUtils;

import org.apache.commons.collections.MapUtils;

import java.io.File;
import java.util.Map;

/**
 * 文件名:CutImage.java
 *
 * @author 作者:孟琬和
 * @version 版本:1.0 类描述:
 */
public class CutImage extends BasesActivity implements OnClickListener {

    /**
     * 标题文字
     */
    private TextView title_topbar;
    /**
     * 返回
     */
    ImageView title_img_back;
    /**
     * 返回
     */
    RelativeLayout title_layout_left;
    /**
     * 整体布局
     */
    RelativeLayout basetitle_bar;
    /**
     * 确定
     */
    TextView home_tv_num;
    /**
     * 确定
     */
    RelativeLayout right_layout;

    private ClipImageLayout mClipImageLayout;
    private String path;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_clipimage);
        byId();
        onclicks();
        initData();

    }

    @Override
    protected BasePresenter createPresenter() {
        return null;
    }

    /**
     * id
     */
    public void byId() {
        title_topbar = (TextView) findViewById(R.id.title_topbar);
        title_img_back = (ImageView) findViewById(R.id.title_img_back);
        title_layout_left = (RelativeLayout) findViewById(R.id.title_layout_left);
        home_tv_num = (TextView) findViewById(R.id.home_tv_num);
        mClipImageLayout = (ClipImageLayout) findViewById(R.id.id_clipImageLayout);
        basetitle_bar = (RelativeLayout) findViewById(R.id.basetitle_bar);
        right_layout = (RelativeLayout) findViewById(R.id.right_layout);

        right_layout.setVisibility(View.VISIBLE);
        home_tv_num.setVisibility(View.VISIBLE);
        title_img_back.setVisibility(View.VISIBLE);
    }

    /**
     * 基本信息
     */
    public void initData() {

        basetitle_bar.setBackgroundColor(Color.parseColor(SystemConfigFactory.getInstance(CutImage.this).getSystemConfig().getColor()));
        title_img_back.setImageResource(R.mipmap.fanhui);
        title_topbar.setText(getString(R.string.xiugai));
        home_tv_num.setText(getString(R.string.ok));

        String intentData = getIntent().getStringExtra("intentData");
        Map<String, Object> data = JsonUtils.fromJsonToCaseInsensitiveMap(intentData);
        path = MapUtils.getString(data, "photopath", "");
        if (TextUtils.isEmpty(path) || !(new File(path).exists())) {
            Toast.makeText(this, "图片加载失败", Toast.LENGTH_SHORT).show();
            return;
        }
        Bitmap bitmap = ImageTools.convertToBitmap(path, 600, 600);
        if (bitmap == null) {
            Toast.makeText(this, "图片加载失败", Toast.LENGTH_SHORT).show();
            return;
        }
        mClipImageLayout.setBitmap(bitmap);
    }

    /***
     * 点击事件
     */
    public void onclicks() {
        title_layout_left.setOnClickListener(this);
        right_layout.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        switch (v.getId()) {
            case R.id.title_layout_left:// 返回
                finish();
                break;
            case R.id.right_layout:// 确定
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        Bitmap bitmap = mClipImageLayout.clip();
                        String path = android.os.Environment.getExternalStorageDirectory() + "/ehomeImg/" + System.currentTimeMillis()
                                + ".png";
                        ImageTools.savePhotoToSDCard(bitmap, path);
                        Intent intent = new Intent();
                        intent.putExtra("result_photo", path);
                        setResult(3, intent);
                        finish();
                    }
                }).start();
                break;
        }
    }

}
